<template>
  <div id="box">
    <h2 style="margin-bottom: 10px;color: black;">在线课程论坛</h2>
    <el-form :model="registForm" label-width="100px" class="registForm">
      <h3 style="margin-bottom: 30px; color: aqua">用户注册</h3>
      <el-form-item label="昵称" prop="username">
        <el-input type="text" placeholder="请输入昵称" v-model="registForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="sno">
        <el-input type="text" @blur="checkUser" placeholder="请输入学号" v-model="registForm.sno" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" placeholder="请输入密码" v-model.number="registForm.password"></el-input><br />
      </el-form-item>
      <el-form-item label="确认密码" prop="rePassword">
        <el-input type="password" placeholder="请确认密码" v-model.number="registForm.rePassword"></el-input><br />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input type="email" placeholder="请输入邮箱" v-model="registForm.email"></el-input><br />
      </el-form-item>
      <el-form-item label="邮箱验证码" prop="checkCode">
        <el-input style="width: 200px;float: left;" placeholder="请输入邮箱验证码" type="text" v-model="registForm.code"></el-input>
        <el-button @click="sendEmail" style="float: right;">发送邮箱</el-button>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" @click="userSubmit">注册</el-button>
        <el-button size="mini" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    <div style="height: 10px;"></div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        registForm: {
          username:'',
          sno: '',
          password: '',
          email: '',
          code: ''
        }
      }
    },
    methods: {
      resetForm: function() {
        this.registForm = ''

      },
      userSubmit() {
        if (this.registForm.code == localStorage.getItem('checkCode')) {
          this.$http
            .post('log/register',{'username':this.registForm.username,'sno':this.registForm.sno,'password':this.registForm.password,'email':this.registForm.email})
            .then(res => {
              if (res.data.statusCode == 200) {
                localStorage.removeItem('checkCode');
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                });
                this.$router.push('/login')
              } else if (res.data.statusCode == 404) {
                this.$message.error(res.data.msg)
              } else if (res.data.statusCode == 500) {
                this.$message.error(res.data.msg)
              }
            })
            .catch(error => {
              this.$message(error)
            })
        }else if(this.registForm.code==null || this.registForm.code==''){
          this.$message.error('验证码为空')
        }else{
          this.$message.error('验证码错误')
        }
      },
      /*发送邮箱信息*/
      sendEmail: function() {
        if (this.registForm.email != null) {

          this.$http
            .post('log/sendEmail?email=' + this.registForm.email)
            .then(res => {
              if (res.data.statusCode == 200) {
                localStorage.setItem('checkCode', res.data.data);
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                })

              } else if (res.data.statusCode == 404) {
                this.$message.error(res.data.msg)
              }
            })
            .catch(error => {
              this.$message(error)
            })
        } else {
          this.$message({
            message: '邮箱不能为空',
            type: danger
          })
        }

      },
      /*验证账户是否存在*/
      checkUser: function() {
        if (this.registForm.sno != null && this.registForm.sno != '') {

          this.$http
            .post('log/checksno?sno=' + this.registForm.sno)
            .then(res => {
              if (res.data.statusCode == 404) {
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                })
              } else if (res.data.statusCode == 200) {
                this.$message.error(res.data.msg)
              }
            })
            .catch(error => {
              this.$message(error)
            });
        }
      }
    }
  }
</script>
<style scoped>
  /*.registForm {
    margin: 20px auto;
    width: 400px;
    background: #fff;
    box-shadow: 0 0 35px #B4BCCC;
    padding: 60px 80px 30px 30px;
    border-radius: 25px;
    margin-top: 50px;
  }*/
  #box{
    background-image: url("http://qsrr7y19m.hn-bkt.clouddn.com/tu2.jfif");
    text-align:center;   /*div盒子内的文字水平居中*/
    background-repeat:no-repeat;
    background-size:1500px 609px ;

    margin:0 auto;    /*div盒子水平居中*/
  }
  .registForm {
    margin: 10px auto;
    width: 400px;
    /*background: #fff;*/
    /*box-shadow: 0 0 35px #B4BCCC;*/
    padding: 60px 80px 30px 30px;
    border-radius: 25px;
    /*margin-top: 100px;*/
    background-color: transparent;
    font-weight:bold

  }
  .login-form el-input {
    background-color: transparent;
    font-weight:bold
  }
</style>
